# Copyright (c) tlpy.
# See LICENSE for details.


"""
This module integrates Tkinter with tlpy.internet's mainloop.

Maintainer: Itamar Shtull-Trauring

To use, do::

    | tksupport.install(rootWidget)

and then run your reactor as usual - do *not* call Tk's mainloop(),
use tlpy's regular mechanism for running the event loop.

Likewise, to stop your program you will need to stop tlpy's
event loop. For example, if you want closing your root widget to
stop tlpy::

    | root.protocol('WM_DELETE_WINDOW', reactor.stop)

When using Aqua Tcl/Tk on Mac OS X the standard Quit menu item in
your application might become unresponsive without the additional
fix::

    | root.createcommand("::tk::mac::Quit", reactor.stop)

@see: U{Tcl/TkAqua FAQ for more info<http://wiki.tcl.tk/12987>}
"""

# system imports
import tkinter, tkinter.simpledialog, tkinter.messagebox

# tlpy imports
from tlpy.python import log
from tlpy.internet import task


_task = None

def install(widget, ms=10, reactor=None):
    """Install a Tkinter.Tk() object into the reactor."""
    installTkFunctions()
    global _task
    _task = task.LoopingCall(widget.update)
    _task.start(ms / 1000.0, False)

def uninstall():
    """Remove the root Tk widget from the reactor.

    Call this before destroy()ing the root widget.
    """
    global _task
    _task.stop()
    _task = None


def installTkFunctions():
    import tlpy.python.util
    tlpy.python.util.getPassword = getPassword


def getPassword(prompt = '', confirm = 0):
    while 1:
        try1 = tkinter.simpledialog.askstring('Password Dialog', prompt, show='*')
        if not confirm:
            return try1
        try2 = tkinter.simpledialog.askstring('Password Dialog', 'Confirm Password', show='*')
        if try1 == try2:
            return try1
        else:
            tkinter.messagebox.showerror('Password Mismatch', 'Passwords did not match, starting over')

__all__ = ["install", "uninstall"]
